﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ArgusLight.Drawing
{
	public class Bitmap
	{
		WriteableBitmap bitmap;

		public Bitmap(int Width, int Height)
		{
			this.bitmap = new WriteableBitmap(Width, Height);
			this.Width = Width;
			this.Height = Height;
		}

		public Bitmap(BitmapSource source)
		{
			this.bitmap = new WriteableBitmap(source);
			this.Width = this.bitmap.PixelWidth;
			this.Height = this.bitmap.PixelHeight;
		}

		public int Width { get; private set; }
		public int Height { get; private set; }

		private int GetIndex(int x, int y)
		{
			return y * this.Width + x;
		}

		private Point GetPoint(int index)
		{
			Point p = new Point();
			p.X = index % this.Width;
			p.Y = index / this.Width;
			return p;
		}

		public Color GetPixel(int x, int y)
		{
			Color c = new Color();
			int i = this.GetIndex(x, y);
			c = c.FromInt(this.bitmap.Pixels[i]);
			return c;
		}

		public void SetPixel(int x, int y, Color c)
		{
			int i = this.GetIndex(x, y);
			this.bitmap.Pixels[i] = c.ToInt();
		}

		public void InvertColor(int x, int y)
		{
			int i = this.GetIndex(x, y);
			this.bitmap.Pixels[i] = 0xFFFFFF - this.bitmap.Pixels[i];
		}

		public WriteableBitmap WritableBitmap { get { return this.bitmap; } }

		public void Invalidate()
		{
			this.bitmap.Invalidate();
		}
	}
}
